package cn.mcsys.viewer.core.listener.thread;

import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

@Component
public class SocketThread extends Thread {

    private Logger log = LoggerFactory.getLogger(SocketThread.class);
    private WebApplicationContext webApplicationContext;

    public SocketThread(WebApplicationContext webApplicationContext){
        this.webApplicationContext = webApplicationContext;
    }

    public SocketThread(){

    }

    @Override
    public void run() {
        BackService backService = webApplicationContext.getBean(BackService.class);
        List<AirRidePoint> airRidePoints = backService.selectAllPoint();
        System.out.println(airRidePoints.size());
//        AirETicketsService bean = webApplicationContext.getBean(AirETicketsService.class);
//        List<AirETickets> airETickets = bean.selectByUserId(22l);
//        System.out.println(airETickets.size());
        ServerSocket serverSocket = null;
        System.out.println("启动线程");
        try {
            serverSocket = new ServerSocket(10001);
            log.info("服务端启动！正在等待客户端连接。。。。。。。。。");
            while (true) {
                Socket socket = null;
                socket = serverSocket.accept();
                log.info("有客户端连接！。。。。。。。。");
//                Writer writer = new OutputStreamWriter(socket.getOutputStream(),"utf-8");
//                BufferedWriter bw = new BufferedWriter(writer);
//                List<AirRidePoint> airRidePoints = backService.selectAllPoint();
//                bw.write(airRidePoints.toString());
//                bw.flush();
//                bw.close();
                Reader reader = new InputStreamReader(socket.getInputStream(),"utf-8");
                BufferedReader br = new BufferedReader(reader);
                String s;
                while ((s=br.readLine())!=null){
                    System.out.println(s);
                }
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
